<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class UserAddRmb extends Model
{
    protected $table = 'useraddrmblog';
    protected $default_table_name = 'useraddrmblog';
    private static $suffix;
    public $timestamps = false;

    public function __construct($suffix = null)
    {
        $this->setConnection('mysql_userlog');
        if (!$suffix) {
            $suffix = date('Ym');
        }
        $this->table = $this->default_table_name . $suffix;
        parent::__construct();
    }

    public function setTable($suffix)
    {
        $table = $this->default_table_name . $suffix;
        if(!Schema::hasTable($table)){
            $table = $this->default_table_name . date('Ym');
        }
        return $this->table = $table;
    }

    /**
     * 日充值总额
     * @param $date
     * @param $server
     * @return mixed
     */
    public function getDayRechargeAmounts($date, $server)
    {
        $this->setTable(date('Ym', $date));
        $res = $this->select('addrmb')->where('receivetime', 'like', date('Ymd', $date) . '%')
            ->where('servername', $server)->sum('addrmb');

        return $res;
    }

    /**
     * 日充值人数
     * @param $date
     * @param $server
     * @return mixed
     */
    public function getDayRechargeUsers($date, $server)
    {
        $this->setTable(date('Ym', $date));
        $res = $this->select('userid')->where('receivetime', 'like', date('Ymd', $date) . '%')
            ->where('servername', $server)->groupBy('userid')->count();

        return $res;
    }

    /**
     * 月充值总额
     * @param $date
     * @param $server
     * @return mixed
     */
    public function getMonthRechargeAmounts($date, $server)
    {
        $this->setTable(date('Ym', $date));
        $res = $this->select('addrmb')->where('receivetime', 'like', date('Ym', $date) . '%')
            ->where('servername', $server)->sum('addrmb');

        return $res;
    }

    /**
     * 日充值次数
     * @param $date
     * @param $server
     * @return mixed
     */
    public function getDayRechargeTimes($date, $server)
    {
        $this->setTable(date('Ym', $date));
        $res = $this->where('receivetime', 'like', date('Ymd', $date) . '%')
            ->where('servername', $server)->count();

        return $res;
    }

    /**
     * 用户日充值金额
     * @param $date
     * @param $server
     * @return array
     */
    public function getDayRechargeAmountsGroupByUser($date, $server)
    {
        $this->setTable(date('Ym', $date));
        $res = $this->select(DB::raw('userid, sum(addrmb) as recharge'))
            ->where('receivetime', 'like', date('Ymd', $date) . '%')
            ->where('servername', $server)
            ->groupBy('userid')->orderBy('recharge', 'desc')
            ->get()->toArray();

        $arr = [];
        $i = 1;
        foreach ($res as $r) {
            $arr[] = [$i, $r['userid'], $r['recharge'] . " 元"];
            $i++;
        }

        return $arr;
    }

    /**
     * 日充值金额每小时分布
     * @param $date
     * @param $server
     * @return array
     */
    public function getDayRechargeAmountsPerHours($date, $server)
    {
        $this->setTable(date('Ym', $date));
        $res = $this->select(DB::raw('sum(addrmb) as recharge, substring(receivetime,9,2) as hour'))
            ->where('receivetime', 'like', date('Ymd', $date) . '%')
            ->where('servername', $server)
            ->groupBy('hour')->orderBy('hour')
            ->get()->toArray();

        $arr = [];
        foreach ($res as $r) {
            $arr[ $r['hour'] ] = $r['recharge'];
        }

        $hours = config('admin.data.hours');
        foreach ($hours as $h) {
            $arr[ $h ] = isset($arr[ $h ]) ? $arr[ $h ] : 0;
        }
        ksort($arr);

        return array_values($arr);
    }
}
